
<?php

class CanBoQLKeHoach extends BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
        return View::make('canbo.qlkh')->with('title',"Quản lý kế hoạch");
	}

	public function index1()
	{
        return View::make('canbo.ghikh')->with('title',"Ghi kế hoạch");
	}
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
        return View::make('canboqlkehoaches.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function lay_chi_tiet_ke_hoach_huong_dan($iddt)
	{
        $chitietkhhuongdan = DB::table('de_tai_luan_van')
        	->join('ke_hoach_huong_dan',
        			function($join) 
        			{
                        $join->on('ke_hoach_huong_dan.id_de_tai','=','de_tai_luan_van.id');
                	}
                 	)
            ->join('chi_tiet_ke_hoach',
            		function($join) 
        			{
                        $join->on('chi_tiet_ke_hoach.id_ke_hoach','=','ke_hoach_huong_dan.id');
                	}
                 	)
            ->join('cong_viec',
            		function($join) 
        			{
                        $join->on('cong_viec.id','=','chi_tiet_ke_hoach.id_cv');
                	}
                 	)
			->select('chi_tiet_ke_hoach.tuan',
					'cong_viec.mo_ta_cong_viec',
					'chi_tiet_ke_hoach.ghi_chu_cong_viec',
					'chi_tiet_ke_hoach.id',
					'chi_tiet_ke_hoach.thoi_gian_danh_gia')
			->where('de_tai_luan_van.id','=',$iddt)                                
			->get();
        return ($chitietkhhuongdan);
	}
	public function lay_chi_tiet_de_tai($iddt)
	{
        $chitietdetai = DB::table('de_tai_luan_van')
        				
        	->select('de_tai_luan_van.ten_de_tai',
					'de_tai_luan_van.mo_ta_de_tai')
			->where('de_tai_luan_van.id','=',$iddt)

			->get();
        return ($chitietdetai);
	}
	public function lay_ke_hoach_huong_dan($iddt)
	{
		$khhuongdan = DB::table('de_tai_luan_van')
        	->join('ke_hoach_huong_dan',
        			function($join)
        			{
        				$join->on('ke_hoach_huong_dan.id_de_tai','=','de_tai_luan_van.id');
        			})			
        	->select('ke_hoach_huong_dan.mo_ta_ke_hoach')
			->where('de_tai_luan_van.id','=',$iddt)                                
			->get();
        return ($khhuongdan);	
	}
	public function getiddetai($tdetai)
    {
        $array = DB::table('de_tai_luan_van')
            ->select('id')
            ->where('ten_de_tai', $tdetai)
            ->first();
        return ($array);    
    }
    public function lay_id_ke_hoach($iddt)
    {
        $array = DB::table('ke_hoach_huong_dan')
            ->select('id')
            ->where('id_de_tai','=',$iddt)
            ->get();
        return ($array);    
    }
    public function lay_mo_ta_cong_viec()
	{
        $motacongviec = DB::table('cong_viec')
        	->select('mo_ta_cong_viec')
		->get();
        return ($motacongviec);
	}
	public function lay_id_cong_viec($motacongviec)
	{
		$idcongviec = DB::table('cong_viec')
        	->select('id')
        	->where('mo_ta_cong_viec','=',$motacongviec)
		->get();
     	return ($idcongviec);
	}
	public function luu_ke_hoach()
	{
		$idkehoach = Input::get('txtIDKeHoach');
		$motakehoach = Input::get('txtMoTaKeHoach');
		$ketqua = DB::table('ke_hoach_huong_dan')
				->where('id', $idkehoach)
             		->update(array(
                			'mo_ta_ke_hoach' => $motakehoach));
				
     	return Redirect::back()->with('ketqualuu', $ketqua);
	}
	public function them_ke_hoach()
	{
		$tuan = Input::get('txtTuan');   
		$iddetai = Input::get('txtIDDetai');     
        	$ghichucongviec = Input::get('txtGhiChu');
        	$motacongviec = Input::get('txtMoTaCongViec');
		$idkehoach = Input::get('txtIDKeHoach');
		
		$dsidcongviec = new CanBoQLKeHoach();
        	$dsidcongviec = $dsidcongviec->lay_id_cong_viec($motacongviec);
        	foreach ($dsidcongviec as $dsidcongviecs) 
        	{
        		$idcongviec = $dsidcongviecs->id;
        	}
        	$ketqua = DB::table('chi_tiet_ke_hoach')
             		->insert(array(
                		'id_ke_hoach' => $idkehoach,
                    	'tuan' => $tuan,
                    	'ghi_chu_cong_viec' => $ghichucongviec,
                    	'id_cv' => $idcongviec));
				
     	return Redirect::back()->with('ketquathem',$ketqua);
	}
	public function sua_ke_hoach()
	{
		$IDChiTietKeHoach = Input::get('txtIDChiTietKeHoach');
		$tuan = Input::get('txtTuan');        
        	$ghichucongviec = Input::get('txtGhiChu');
        	$motacongviec = Input::get('txtMoTaCongViec');
		
        	$dsidcongviec = new CanBoQLKeHoach();
        	$dsidcongviec = $dsidcongviec->lay_id_cong_viec($motacongviec);
        	
        	foreach ($dsidcongviec as $dsidcongviecs) 
        	{
        		$idcongviec = $dsidcongviecs->id;
        	}
        	$ketqua = DB::table('chi_tiet_ke_hoach')
			->where('id', $IDChiTietKeHoach)
             	->update(
                	array(
                	'tuan' => $tuan,
                    'ghi_chu_cong_viec' => $ghichucongviec,
                    'id_cv' => $idcongviec)
                     	);
     	return Redirect::back()->with('ketquasua',$ketqua);
	}
	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	/*public function edit($iddt)
	{
        DB::table('de_tai_luan_van')
        	->where('id', $iddt)
        	->update(array(''))
	}*/

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
